﻿using System.Web;
using System.Web.Mvc;

namespace P7DC.Common.Attribute
{
	public abstract class AbstractSessionExpireAttribute : ActionFilterAttribute
	{
		public override void OnActionExecuting(ActionExecutingContext filterContext)
		{
			HttpContextBase ctx = filterContext.HttpContext;
			if (ctx.Session != null && ctx.Session.IsNewSession)
			{
				// If it says it is a new session, but an existing cookie exists, then it must
				// have timed out
				string sessionCookie = ctx.Request.Headers["Cookie"];
				if ((null != sessionCookie) && (sessionCookie.IndexOf("ASP.NET_SessionId") >= 0))
				{
					filterContext.Result = OnSessionExpiryRedirectResult(filterContext);
				}
			}
			base.OnActionExecuting(filterContext);
		}

		public abstract ActionResult OnSessionExpiryRedirectResult(ActionExecutingContext filterContext);
	}
}